<?php

class Myusersubscribe {

    public $CI;
    public $base_url;

    public function __construct()
    {
        $this->CI = & get_instance();
        $this->CI->load->model('userv2info_model');
        $this->CI->load->model('userv2_model');
        $this->CI->load->library('MyEmail');
        $this->CI->load->library('PHPEmail');
        //$this->base_url = $this->CI->config->item('base_url');
    }

    public function user_activation($uid,$email,$createTime,$mail_type='tourist')
    {
        //保存用户激活token
        $data['mailToken'] = md5($createTime.time());
        $data['token_exptime'] = time()+60*60*24;
        $data['email'] = $email;
        $data['active'] = 0;

        $this->CI->userv2_model->editUser($data, array('uid' => $uid));

        //发送邮件
        if($mail_type == 'guider'){
            $email_body = '邮件激活地址： {unwrap}'.$this->base_url."/ucenter/confirm_token/?token=".$data['mailToken'].' {/unwrap} 如果链接不能直接打开，请复制到浏览器中打开';
        }else if($mail_type == 'tourist'){
            $email_body = '邮件激活地址： {unwrap}'.$this->base_url."/ucenter/confirm_token/?token=".$data['mailToken'].' {/unwrap} 如果链接不能直接打开，请复制到浏览器中打开';
        }else{
            $email_body = '邮件激活地址： {unwrap}'.$this->base_url."/ucenter/confirm_token/?token=".$data['mailToken'].' {/unwrap} 如果链接不能直接打开，请复制到浏览器中打开';
        }

        $data['status'] = $this->CI->myemail->sendEmail($email, '用户激活', $email_body);

        $match_qq = "#@qq\.com#i";
        $match_163 = "#@163\.com#i";
        $match_waya = "#@wayatrip\.com#i";
        $match_sina = "#@sina\.com.*#i";
        $match_sohu = "#@sohu\.com.*#i";
        $match_126 = "#@126\.com.*#i";
        $match_hotmail = "#@hotmail\.com.*#i";
        $match_outlook = "#@outlook\.com.*#i";
        $match_yahoo = "#@yahoo\.com.*#i";
        $match_gmail = "#@gmail\.com.*#i";
        $match_189 = "#@189\.cn.*#i";
        $match_139 = "#@139\.com.*#i";
        $match_tom = "#@tom\.com.*#i";


        if(preg_match($match_qq,$email)){
            $data['url'] = "https://mail.qq.com";
        }
        elseif(preg_match($match_163,$email)){
            $data['url'] = "http://mail.163.com/";
        }
        elseif(preg_match($match_waya,$email)){
            $data['url'] = "http://qiye.163.com/login/";
        }
        elseif(preg_match($match_sina,$email)){
            $data['url'] = "http://mail.sina.com.cn/";
        }
        elseif(preg_match($match_sohu,$email)){
            $data['url'] = "http://mail.sohu.com/";
        }
        elseif(preg_match($match_126,$email)){
            $data['url'] = "http://mail.126.com/";
        }
        elseif(preg_match($match_hotmail,$email)||preg_match($match_outlook,$email)){
            $data['url'] = "http://www.hotmail.com/";
        }
        elseif(preg_match($match_yahoo,$email)){
            $data['url'] = "http://login.yahoo.com/";
        }
        elseif(preg_match($match_gmail,$email)){
            $data['url'] = "http://www.gmail.com/";
        }
        elseif(preg_match($match_189,$email)){
            $data['url'] = "http://webmail30.189.cn/";
        }
        elseif(preg_match($match_139,$email)){
            $data['url'] = "http://mail.10086.cn/";
        }
        elseif(preg_match($match_tom,$email)){
            $data['url'] = "http://web.mail.tom.com/";
        }

        else{
            $data['url'] = "";
        }

        return $data;
    }


    public function user_activation_code($email)
    {
        $code = mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9);

        //发送邮件
        $body = '您的邮箱验证码为:'.$code."，10分钟内有效，为保证您的账户安全，请尽快完成箱地址的修改";       
        $res=$this->CI->phpemail->to($email)
        ->sender("哇吖旅行")
        ->subject("修改邮箱地址")
        ->message($body)
        ->send();
        
        if($res){
            if(!isset($_SESSION)) session_start();
            $_SESSION['email_code'] = $code;
            return true;
        }else{
            return false;
        }


    }

    /**
     * 验证邮箱
     *
     * @param $token
     * @return array
     */
    public function confirm_token($token)
    {
        $data = $this->CI->userv2_model->getUserv2('mailToken = "'.$token.'"');
        if (empty($data)) {
            $msg = "验证失败";
            return array(0,$msg);
        } else {
            if ($data['active'] == 1) {
                $msg = "验证完成，无须再次验证";
                return array(2,$msg);
            } else {
                if (time() > $data['token_exptime']) {
                    $msg = "邮件过期";
                    return array(0,$msg);
                } else {
                    $msg = "验证通过";
                    $save['active'] = 1;
                    $save['userId'] = $data['email'];
                    $this->CI->userv2_model->editUser($save, array('mailToken'=> $token));
                    $data['msg'] = $msg;
                    return array(1,$data);
                }
            }
        }
    }
}
